package com.gko.encoder;

import java.util.ArrayList;
import java.util.List;

/**
 * The class used for the generation of strings from the given sets of characters.
 * 
 * @author gheorghi.kozlovskii
 * */
public class StringEncoder {

	/**
	 * Generates the all possible character combinations from the given mapping combinations.
	 * 
	 * @param mappingCombinations
	 * @author gheorghi.kozlovskii
	 * */
	public List<String> encode(List<char[]> mappingCombinations) {

		int max = mappingCombinations.size() - 1;
		List<String> result = new ArrayList<String>();

		helper("", 0, mappingCombinations, max, result);

		return result;

	}

	private void helper(final String arr, final int index,
			final List<char[]> mappingCombinations, final int max,
			List<String> result) {

		int combinationLength = mappingCombinations.get(index).length;
		for (int i = 0; i < combinationLength; i++) {
			String generatedCharacterString = new String(arr);

			generatedCharacterString += String.valueOf(mappingCombinations
					.get(index)[i]);
			
			if (index == max) {
				processGeneratedString(generatedCharacterString, result);
			} else {
				helper(generatedCharacterString, index + 1,
						mappingCombinations, max, result);
			}
		}
	}

	protected void processGeneratedString(String generatedCharacterString,
			List<String> result) {
		result.add(generatedCharacterString);
	}
}
